package com.ruoyi.dl.controller.wechat;

import com.ruoyi.dl.uitls.Tool;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @FileName: WeChatUpLoadController
 * @program: wangshouli
 * @description: 微信文件上传
 * @author: Mr.Wang
 * @create: 2021-01-05 15:37
 * @version: V1.0
 **/
@Controller
@RequestMapping("/wechat/upload")
@CrossOrigin(allowCredentials = "true")
public class WeChatUpLoadController {

    @RequestMapping(value = "/fileUpload", produces = "application/json")
    @ResponseBody
    public String wxSubject(HttpServletRequest request, @RequestParam("file") MultipartFile files) {
        String id = "";
        String type = "";
        try {
            id = request.getParameter("id");
            type = request.getParameter("type");
            if (type == null || type == "") {
                type = "";
            }
            System.out.println("id=" + id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 构建上传目录路径
        // request.getServletContext().getRealPath("/upload");
        String uploadPath = Tool.UPLOAD_PATH;//你自己保存音频的URL;
        String savePath = "/avatar/" + id + type;
        // 如果目录不存在就创建
        File uploadDir = new File(uploadPath + savePath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        if (uploadDir.isDirectory()) {
            String[] tempList = uploadDir.list();
            if (tempList.length > 0) {
                for (String string : tempList) {
                    File tempFile = new File(uploadPath + savePath + string);
                    if (tempFile.isFile()) {
                        tempFile.delete();
                    }
                }
            }
        }
        // 获取文件的 名称.扩展名
        String oldName = files.getOriginalFilename();
        String extensionName = "";
        // 获取原来的扩展名
        if ((oldName != null) && (oldName.length() > 0)) {
            int dot = oldName.lastIndexOf('.');
            if ((dot > -1) && (dot < (oldName.length() - 1))) {
                extensionName = oldName.substring(dot);
            }
        }
        // 构建文件名称
        String fileName = System.currentTimeMillis() + "_" + System.nanoTime()
                + extensionName;
        // 构建文件路径
        String filePath = uploadPath + savePath + "/" + fileName;
        String newFilePath = "/profile" + savePath + fileName;
        // 保存文件
        try {
            FileUtils.writeByteArrayToFile(new File(filePath),
                    files.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("接收文件成功 " + newFilePath);
        return newFilePath;
    }
}
